import 'package:flutter/material.dart';
import 'package:flutter_chip/database/manager/db_manager.dart';
import 'package:sqflite/sqflite.dart';

abstract class BaseDbProvider {
  bool isTableExits = false;

  tableSqlString();

  tableName();

  tableBaseString(String name, String columnId) {
    return "create table $name($columnId integer primary key autoincrement,";
  }

  Future<Database> getDataBase() async {
    return await open();
  }

  @mustCallSuper
  prepare(name, String createSql) async {
    isTableExits = await DBManager.isTableExists(name);
    if (!isTableExits) {
      print("创建表语句：$createSql");
      Database db = await DBManager.getCurrentDatabase();
      return db.execute(createSql);
    }
  }

  @mustCallSuper
  open() async {
    if (!isTableExits) {
      await prepare(tableName(), tableSqlString());
    }
    return await DBManager.getCurrentDatabase();
  }
}
